import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 编码格式，文件后缀名为.avi
# 定视频的宽度和高度 帧率：每秒的帧数
out = cv2.VideoWriter("../images/output.avi", fourcc, 20.0, (640, 480))
if cap.isOpened():
    open, frame = cap.read()
else:
    open = False
while open:
    ret, frame = cap.read()
    if frame is None:
        break
    if ret == True:
        out.write(frame)
        cv2.imshow("frame", frame) # 显示视频
        if cv2.waitKey(1) & 0xff == 27:
            break
cap.release()
cv2.destroyAllWindows()